@echo off

set time_begin=%time:~0,-3%
echo start time: %time_begin%
for /f "tokens=1,2,3 delims=:" %%i in ("%time_begin%") do (
set /a hour_b=%%i
set /a minute_b=%%j
set /a second_b=%%k
)

set QT_VERSION=D:\Qt\Qt5.9.3\5.9.3

set SOURCE_DIR=%cd%\..
set BUILD_DIR=%SOURCE_DIR%\build\mingw
set PREFIX_DIR=%SOURCE_DIR%\build\install

set BUILD_DEBUG=OFF
set BUILD_RELEASE=ON

set BUILD_PACK=ON

::goto :pack

call "%QT_VERSION%\mingw53_32\bin\qtenv2.bat"

echo +=============================
echo Start compiling.... It will take about 15 minutes ......
echo +=============================

echo +=============================
echo Start qmake build %QT_VERSION% mingw Win32 Debug and Release ......
echo +=============================


if "x%BUILD_DEBUG%" == "xOFF" goto :release

mkdir %BUILD_DIR%\x32\debug
cd /D %BUILD_DIR%\x32\debug

del *.* /s /f /q
::rd . /s /q

qmake %SOURCE_DIR%\build-all-p3000.pro "CONFIG+=debug" "CYG_PREFIX+=%PREFIX_DIR%" >> "qmake-build-mingw.log"
mingw32-make && mingw32-make install >> "qmake-build-mingw.log"

:release
if "x%BUILD_RELEASE%" == "xOFF" goto :end

mkdir %BUILD_DIR%\x32\release
cd /D %BUILD_DIR%\x32\release

del *.* /s /f /q
::rd . /s /q

qmake %SOURCE_DIR%\build-all-p3000.pro "CONFIG+=release" "CYG_PREFIX+=%PREFIX_DIR%" >> "qmake-build-mingw.log"
mingw32-make && mingw32-make install >> "qmake-build-mingw.log"

echo +=============================
echo Start package programs p3000......
echo +=============================
cd %SOURCE_DIR%

:pack
if "x%BUILD_PACK%" == "xOFF" goto :packend

for /f "tokens=3*" %%i in ('find "VERSION_BUILD_PTS" %SOURCE_DIR%\version.h') do set PTS=%%i
for /f "tokens=3*" %%i in ('find "VERSION_GIT "      %SOURCE_DIR%\version.h') do set GIT=%%i
for /f "tokens=3*" %%i in ('find "VERSION_SVN "      %SOURCE_DIR%\version.h') do set SVN=%%i

if "x%GIT%" == "x1" for /f "tokens=3*" %%i in ('find "VERSION_GIT_TAG "  %SOURCE_DIR%\version.h') do (set TAG=%%i)
if "x%GIT%" == "x1" for /f "tokens=3*" %%i in ('find "VERSION_GIT_SHA1 " %SOURCE_DIR%\version.h') do (set REV=%%i)

if "x%SVN%" == "x1" for /f "tokens=3*" %%i in ('find "VERSION_SVN_TAG "  %SOURCE_DIR%\version.h') do (set TAG=%%i)
if "x%SVN%" == "x1" for /f "tokens=3*" %%i in ('find "VERSION_SVN_REV "  %SOURCE_DIR%\version.h') do (set REV=%%i)

if "x%BUILD_DEBUG%" == "xOFF" goto :packrelease

set INST_DATA_DIR=%PREFIX_DIR%\mingw\x32\p3000d
if not exist "%INST_DATA_DIR%" goto :packrelease

set PACK_DIR=%SOURCE_DIR%\scripts\package\windows\debug
set PACK_CFG_DIR=%PACK_DIR%\config
set PACK_PKG_DIR=%PACK_DIR%\packages
set PACK_DATA_DIR=%PACK_DIR%\packages\com.contron.p3000d\data

if exist "%PACK_DATA_DIR%" rmdir "%PACK_DATA_DIR%"
mklink /j "%PACK_DATA_DIR%" "%INST_DATA_DIR%"

set STROLD=PACK_VERSION
set STRNEW=%TAG:"=%-%REV:"=%
setlocal enabledelayedexpansion
if exist "%PACK_CFG_DIR%\config.xml" del "%PACK_CFG_DIR%\config.xml" /f
for /f "tokens=*" %%i in (%PACK_CFG_DIR%\config.xml.bak) do (
    set "var=%%i"
    if not !var!.==. (
        set "var=!var:%STROLD%=%STRNEW%!"
        echo !var!! >> %PACK_CFG_DIR%\config.xml
    )
)

%SOURCE_DIR%\..\tools\QtIFW\binarycreator.exe -c %PACK_CFG_DIR%\config.xml -p %PACK_PKG_DIR%\ %PREFIX_DIR%\..\p3000d-bin-%TAG%-%REV%-build%PTS%-win32.exe -v >> %BUILD_DIR%\x32\debug\qmake-build-mingw.log
rmdir "%PACK_DATA_DIR%"
del "%PACK_CFG_DIR%\config.xml" /f

:packrelease
if "x%BUILD_RELEASE%" == "xOFF" goto :packend

set INST_DATA_DIR=%PREFIX_DIR%\mingw\x32\p3000
if not exist "%INST_DATA_DIR%"  goto :packend

set PACK_DIR=%SOURCE_DIR%\scripts\package\windows\release
set PACK_CFG_DIR=%PACK_DIR%\config
set PACK_PKG_DIR=%PACK_DIR%\packages
set PACK_DATA_DIR=%PACK_DIR%\packages\com.contron.p3000\data

if exist "%PACK_DATA_DIR%" rmdir "%PACK_DATA_DIR%"
mklink /j "%PACK_DATA_DIR%" "%INST_DATA_DIR%"

set STROLD=PACK_VERSION
set STRNEW=%TAG:"=%-%REV:"=%
setlocal enabledelayedexpansion
if exist "%PACK_CFG_DIR%\config.xml" del "%PACK_CFG_DIR%\config.xml" /f
for /f "tokens=*" %%i in (%PACK_CFG_DIR%\config.xml.bak) do (
    set "var=%%i"
    if not !var!.==. (
        set "var=!var:%STROLD%=%STRNEW%!"
        echo !var!! >> %PACK_CFG_DIR%\config.xml
    )
)

%SOURCE_DIR%\..\tools\QtIFW\binarycreator.exe -c %PACK_CFG_DIR%\config.xml -p %PACK_PKG_DIR%\ %PREFIX_DIR%\..\p3000-bin-%TAG%-%REV%-build%PTS%-win32.exe -v >> %BUILD_DIR%\x32\release\qmake-build-mingw.log
rmdir "%PACK_DATA_DIR%"
del "%PACK_CFG_DIR%\config.xml" /f

:packend

echo +=============================
echo Build finished
echo +=============================

:end

set time_end=%time:~0,-3%
for /f "tokens=1,2,3 delims=:" %%i in ("%time_end%") do (
set /a hour_e=%%i
set /a minute_e=%%j
set /a second_e=%%k
)

call :time_lapse
echo cost: %hour_% hour %minute_% munite %second_% second
pause

goto :eof
:time_lapse
if %second_e% lss %second_b% (
set /a minute_e=%minute_e%-1
set /a second_e=%second_e%+60
)
set /a second_=%second_e%-%second_b%
if %minute_e% lss %minute_b% (
set /a hour_e=%hour_e%-1
set /a minute_e=%minute_e%+60
)
set /a minute_=%minute_e%-%minute_b%
if %hour_e% lss %hour_b% (
set /a hour_e=%hour_e%+24
)
set /a hour_=%hour_e%-%hour_b%
